# cost within budget
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 读取CSV文件
df = pd.read_csv('PerformanceTest_ReviseTaskLevel.csv')

# 提取所需列
data = df[['Recipe', 'TaskNumber', 'Algorithm', 'WithinBudget']]

# 计算每个工作流中各个算法的超支比例
WithinBudget_stats = data.groupby(['Recipe', 'TaskNumber', 'Algorithm'])['WithinBudget'].mean().reset_index()
WithinBudget_stats['WithinBudget_Rate'] = WithinBudget_stats['WithinBudget'] * 100

# 设置图形样式
plt.style.use('default')
sns.set_palette("Set2")

# 获取所有工作流
recipes = data['Recipe'].unique()

# 为每个工作流创建超支比例柱状图
for recipe in sorted(recipes):
    # 筛选当前工作流的数据
    recipe_data = WithinBudget_stats[WithinBudget_stats['Recipe'] == recipe]
    
    # 创建图形
    plt.figure(figsize=(10, 6))
    
    # 绘制柱状图
    ax = sns.barplot(data=recipe_data, x='TaskNumber', y='WithinBudget_Rate', hue='Algorithm')
    
    # 设置标题和标签
    plt.title(f'Within Budget by Recipe {recipe}', fontsize=16, fontweight='bold')
    plt.xlabel('TaskNumber', fontsize=12)
    plt.ylabel('Rate (%)', fontsize=12)
    
    plt.ylim(0, 105)
    
    # 在柱子上添加数值标签
    for container in ax.containers:
        ax.bar_label(container, fmt='%.1f%%', fontsize=9)
    
    # 添加图例
    plt.legend(title='Altorigim', bbox_to_anchor=(1.05, 1), loc='upper left')
    
    # 调整布局
    plt.tight_layout()
    
    # 显示图表
    plt.show()

# 打印统计结果
print("成本遵守情况统计:")
print(WithinBudget_stats.to_string(index=False, float_format='%.2f'))

#      Recipe  TaskNumber   Algorithm  WithinBudget  WithinBudget_Rate
#       blast         100          CG          0.00               0.00
#       blast         100         DQN          1.00             100.00
#       blast         100    GRP-HEFT          1.00             100.00
#       blast         100 GRP-HEFT_HE          1.00             100.00
#       blast         100       HGDQN          1.00             100.00
#       blast         125          CG          0.00               0.00
#       blast         125         DQN          1.00             100.00
#       blast         125    GRP-HEFT          1.00             100.00
#       blast         125 GRP-HEFT_HE          1.00             100.00
#       blast         125       HGDQN          1.00             100.00
#       blast         150          CG          0.00               0.00
#       blast         150         DQN          1.00             100.00
#       blast         150    GRP-HEFT          1.00             100.00
#       blast         150 GRP-HEFT_HE          1.00             100.00
#       blast         150       HGDQN          1.00             100.00
#       blast         175          CG          0.00               0.00
#       blast         175         DQN          1.00             100.00
#       blast         175    GRP-HEFT          1.00             100.00
#       blast         175 GRP-HEFT_HE          1.00             100.00
#       blast         175       HGDQN          0.98              98.00        
#       blast         200          CG          0.00               0.00        
#       blast         200         DQN          1.00             100.00        
#       blast         200    GRP-HEFT          1.00             100.00        
#       blast         200 GRP-HEFT_HE          1.00             100.00        
#       blast         200       HGDQN          1.00             100.00        
#         bwa         125          CG          0.00               0.00        
#         bwa         125         DQN          0.10              10.00        
#         bwa         125    GRP-HEFT          0.54              54.00        
#         bwa         125 GRP-HEFT_HE          1.00             100.00        
#         bwa         125       HGDQN          0.00               0.00        
#         bwa         150          CG          0.00               0.00        
#         bwa         150         DQN          0.06               6.00        
#         bwa         150    GRP-HEFT          0.52              52.00        
#         bwa         150 GRP-HEFT_HE          1.00             100.00        
#         bwa         150       HGDQN          0.00               0.00        
#         bwa         175          CG          0.00               0.00        
#         bwa         175         DQN          0.00               0.00        
#         bwa         175    GRP-HEFT          0.58              58.00        
#         bwa         175 GRP-HEFT_HE          1.00             100.00        
#         bwa         175       HGDQN          0.02               2.00        
#         bwa         200          CG          0.00               0.00        
#         bwa         200         DQN          0.00               0.00        
#         bwa         200    GRP-HEFT          0.54              54.00        
#         bwa         200 GRP-HEFT_HE          1.00             100.00        
#         bwa         200       HGDQN          0.00               0.00        
#      cycles         100          CG          0.00               0.00        
#      cycles         100         DQN          0.04               4.00        
#      cycles         100    GRP-HEFT          0.00               0.00        
#      cycles         100 GRP-HEFT_HE          1.00             100.00        
#      cycles         125          CG          0.00               0.00        
#      cycles         125         DQN          0.04               4.00        
#      cycles         125    GRP-HEFT          0.00               0.00        
#      cycles         125 GRP-HEFT_HE          1.00             100.00        
#      cycles         150          CG          0.00               0.00        
#      cycles         150         DQN          0.06               6.00        
#      cycles         150    GRP-HEFT          0.00               0.00        
#      cycles         150 GRP-HEFT_HE          1.00             100.00        
#      cycles         175          CG          0.00               0.00        
#      cycles         175         DQN          0.00               0.00        
#      cycles         175    GRP-HEFT          0.00               0.00        
#      cycles         175 GRP-HEFT_HE          1.00             100.00        
#      cycles         200          CG          0.00               0.00        
#      cycles         200         DQN          0.00               0.00        
#      cycles         200    GRP-HEFT          0.00               0.00        
#      cycles         200 GRP-HEFT_HE          1.00             100.00        
# epigenomics         100          CG          0.00               0.00        
# epigenomics         100         DQN          0.16              16.00        
# epigenomics         100    GRP-HEFT          0.00               0.00        
# epigenomics         100 GRP-HEFT_HE          1.00             100.00        
# epigenomics         125          CG          0.00               0.00        
# epigenomics         125         DQN          0.04               4.00        
# epigenomics         125    GRP-HEFT          0.00               0.00        
# epigenomics         125 GRP-HEFT_HE          1.00             100.00        
# epigenomics         150          CG          0.00               0.00        
# epigenomics         150         DQN          0.00               0.00        
# epigenomics         150    GRP-HEFT          0.00               0.00        
# epigenomics         150 GRP-HEFT_HE          1.00             100.00        
# epigenomics         175          CG          0.00               0.00        
# epigenomics         175         DQN          0.13              13.33        
# epigenomics         175    GRP-HEFT          0.00               0.00        
# epigenomics         175 GRP-HEFT_HE          1.00             100.00        
# epigenomics         200          CG          0.00               0.00        
# epigenomics         200         DQN          0.18              18.00        
# epigenomics         200    GRP-HEFT          0.00               0.00        
# epigenomics         200 GRP-HEFT_HE          1.00             100.00        
#      genome         100          CG          0.00               0.00        
#      genome         100         DQN          0.02               2.00        
#      genome         100    GRP-HEFT          0.02               2.00        
#      genome         100 GRP-HEFT_HE          1.00             100.00        
#      genome         125          CG          0.04               4.00        
#      genome         125         DQN          0.04               4.00        
#      genome         125    GRP-HEFT          0.00               0.00        
#      genome         125 GRP-HEFT_HE          1.00             100.00        
#      genome         150          CG          0.00               0.00        
#      genome         150         DQN          0.06               6.00        
#      genome         150    GRP-HEFT          0.08               8.00        
#      genome         150 GRP-HEFT_HE          1.00             100.00        
#      genome         175          CG          0.16              16.00        
#      genome         175         DQN          0.12              12.00        
#      genome         175    GRP-HEFT          0.00               0.00        
#      genome         175 GRP-HEFT_HE          1.00             100.00        
#      genome         200          CG          0.00               0.00        
#      genome         200         DQN          0.02               2.00        
#      genome         200    GRP-HEFT          0.02               2.00        
#      genome         200 GRP-HEFT_HE          1.00             100.00        
#     montage         100          CG          0.00               0.00        
#     montage         100         DQN          1.00             100.00        
#     montage         100    GRP-HEFT          0.62              62.00        
#     montage         100 GRP-HEFT_HE          1.00             100.00        
#     montage         100       HGDQN          0.90              90.00        
#     montage         125          CG          0.00               0.00        
#     montage         125         DQN          1.00             100.00        
#     montage         125    GRP-HEFT          0.80              80.00        
#     montage         125 GRP-HEFT_HE          1.00             100.00        
#     montage         125       HGDQN          0.96              96.00        
#     montage         150          CG          0.00               0.00        
#     montage         150         DQN          0.98              97.78        
#     montage         150    GRP-HEFT          0.73              73.33        
#     montage         150 GRP-HEFT_HE          1.00             100.00        
#     montage         150       HGDQN          0.91              91.11        
#     montage         175          CG          0.00               0.00        
#     montage         175         DQN          1.00             100.00        
#     montage         175    GRP-HEFT          0.78              78.00        
#     montage         175 GRP-HEFT_HE          1.00             100.00        
#     montage         175       HGDQN          0.84              84.00        
#     montage         200          CG          0.00               0.00        
#     montage         200         DQN          0.98              98.00        
#     montage         200    GRP-HEFT          0.82              82.00        
#     montage         200 GRP-HEFT_HE          1.00             100.00        
#     montage         200       HGDQN          1.00             100.00        
#      rnaseq          63          CG          1.00             100.00        
#      rnaseq          63         DQN          0.56              56.00        
#      rnaseq          63    GRP-HEFT          0.00               0.00        
#      rnaseq          63 GRP-HEFT_HE          1.00             100.00        
#      rnaseq         125          CG          0.80              80.00        
#      rnaseq         125         DQN          0.44              44.00        
#      rnaseq         125    GRP-HEFT          0.00               0.00        
#      rnaseq         125 GRP-HEFT_HE          1.00             100.00        
#      rnaseq         189          CG          0.40              40.00        
#      rnaseq         189         DQN          0.16              16.00        
#      rnaseq         189    GRP-HEFT          0.00               0.00        
#      rnaseq         189 GRP-HEFT_HE          1.00             100.00        
#  seismology         100          CG          0.56              56.00        
#  seismology         100         DQN          0.38              38.00        
#  seismology         100    GRP-HEFT          0.00               0.00        
#  seismology         100 GRP-HEFT_HE          1.00             100.00        
#  seismology         125          CG          0.46              46.00        
#  seismology         125         DQN          0.20              20.00        
#  seismology         125    GRP-HEFT          0.00               0.00        
#  seismology         125 GRP-HEFT_HE          1.00             100.00        
#  seismology         150          CG          0.06               6.00        
#  seismology         150         DQN          0.02               2.00        
#  seismology         150    GRP-HEFT          0.00               0.00        
#  seismology         150 GRP-HEFT_HE          1.00             100.00        
#  seismology         175          CG          0.40              40.00        
#  seismology         175         DQN          0.26              26.00        
#  seismology         175    GRP-HEFT          0.00               0.00        
#  seismology         175 GRP-HEFT_HE          1.00             100.00        
#  seismology         200          CG          0.38              38.00        
#  seismology         200         DQN          0.10              10.00        
#  seismology         200    GRP-HEFT          0.00               0.00        
#  seismology         200 GRP-HEFT_HE          1.00             100.00        
#       soykb         100          CG          0.00               0.00        
#       soykb         100         DQN          0.48              48.00        
#       soykb         100    GRP-HEFT          0.52              52.00        
#       soykb         100 GRP-HEFT_HE          1.00             100.00        
#       soykb         100       HGDQN          0.52              52.00        
#       soykb         125          CG          0.00               0.00        
#       soykb         125         DQN          0.40              40.00        
#       soykb         125    GRP-HEFT          0.70              70.00        
#       soykb         125 GRP-HEFT_HE          1.00             100.00        
#       soykb         125       HGDQN          0.56              56.00        
#       soykb         150          CG          0.00               0.00        
#       soykb         150         DQN          0.36              36.00        
#       soykb         150    GRP-HEFT          0.12              12.00        
#       soykb         150 GRP-HEFT_HE          1.00             100.00        
#       soykb         150       HGDQN          0.52              52.00        
#       soykb         175          CG          0.00               0.00        
#       soykb         175         DQN          0.36              36.00        
#       soykb         175    GRP-HEFT          0.20              20.00        
#       soykb         175 GRP-HEFT_HE          1.00             100.00        
#       soykb         175       HGDQN          0.52              52.00        
#       soykb         200          CG          0.00               0.00        
#       soykb         200         DQN          0.22              22.00        
#       soykb         200    GRP-HEFT          0.06               6.00        
#       soykb         200 GRP-HEFT_HE          1.00             100.00        
#       soykb         200       HGDQN          0.58              58.00        
#   srasearch         100          CG          0.00               0.00        
#   srasearch         100         DQN          1.00             100.00        
#   srasearch         100    GRP-HEFT          0.74              74.00        
#   srasearch         100 GRP-HEFT_HE          1.00             100.00        
#   srasearch         125          CG          0.00               0.00        
#   srasearch         125         DQN          1.00             100.00        
#   srasearch         125    GRP-HEFT          0.68              68.00        
#   srasearch         125 GRP-HEFT_HE          1.00             100.00        
#   srasearch         150          CG          0.00               0.00        
#   srasearch         150         DQN          1.00             100.00        
#   srasearch         150    GRP-HEFT          0.70              70.00        
#   srasearch         150 GRP-HEFT_HE          1.00             100.00        
#   srasearch         175          CG          0.00               0.00        
#   srasearch         175         DQN          1.00             100.00        
#   srasearch         175    GRP-HEFT          0.72              72.00        
#   srasearch         175 GRP-HEFT_HE          1.00             100.00        
#   srasearch         200          CG          0.00               0.00        
#   srasearch         200         DQN          1.00             100.00        
#   srasearch         200    GRP-HEFT          0.70              70.00        
#   srasearch         200 GRP-HEFT_HE          1.00             100.00        